//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.ponly.fs.util;

import java.io.*;
import java.lang.reflect.UndeclaredThrowableException;
import java.math.BigInteger;
import java.net.*;
import java.nio.charset.Charset;
import java.security.spec.RSAKeyGenParameterSpec;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.zip.CRC32;

public abstract class Throwables {
    private static final String __LICENSE_DATA = "__NULL__";

    private Throwables() {
    }

    public static boolean causedBy(Throwable t, Class<? extends Throwable> causeType) {
        for(HashSet causes = new HashSet(); t != null && !causeType.isInstance(t) && !causes.contains(t); t = t.getCause()) {
            causes.add(t);
        }

        return t != null && causeType.isInstance(t);
    }

    public static <R> R rethrowRuntimeException(Throwable ex) {
        if(ex instanceof RuntimeException) {
            throw (RuntimeException)ex;
        } else if(ex instanceof Error) {
            throw (Error)ex;
        } else {
            throw new UndeclaredThrowableException(ex);
        }
    }

    public static <R> R rethrowException(Throwable ex) throws Exception {
        if(ex instanceof Exception) {
            throw (Exception)ex;
        } else if(ex instanceof Error) {
            throw (Error)ex;
        } else {
            throw new UndeclaredThrowableException(ex);
        }
    }

    public static String getStackTraceAsString(Throwable t) {
        StringWriter writer = new StringWriter();
        t.printStackTrace(new PrintWriter(writer));
        return writer.toString();
    }

    static {
        BigInteger PUBLIC_MOD = new BigInteger("86f71688cdd2612ca117d1f54bdae029", 16);
        BigInteger PUBLIC_EXP = RSAKeyGenParameterSpec.F4;
        String machineId = __getMachineId();
        String fullLicense = System.getProperty("ponly.license");
        if(fullLicense == null || "".equals(fullLicense)) {
            fullLicense = __fetchLicense(machineId);
        }

        try {
            String ex = fullLicense;
            String message = "License invalid: " + machineId + ":" + fullLicense;
            int i = fullLicense.indexOf("-");
            int customerId = -1;
            if(5 == i) {
                customerId = Integer.parseInt(fullLicense.substring(0, i));
                ex = fullLicense.substring(i + 1);
            }

            BigInteger lic = null;

            try {
                lic = __decodeGroups(ex).modPow(PUBLIC_EXP, PUBLIC_MOD);
            } catch (IllegalStateException var21) {
                throw new IllegalStateException(message);
            }

            byte[] bytes = lic.toByteArray();
            short checksum = __calculateCRC32(machineId, customerId, bytes);
            if(bytes[10] == (byte)(checksum & 255) && bytes[11] == (byte)(checksum >> 8 & 255)) {
                int licenseType = bytes[0] >> 4 & 15;
                int productId = bytes[0] & 15;
                int majorVersion = bytes[1] & 15;
                int minorVersion = bytes[1] >> 4 & 15;
                long generationTime = ((long)bytes[2] & 255L) + (long)((bytes[3] & 255) << 8) + (long)((bytes[4] & 255) << 16) + (long)((bytes[5] & 255) << 24) << 16;
                int days = (bytes[6] & 255) + ((bytes[7] & 255) << 8);
                Date expirationDate = days != 0?new Date(generationTime + (long)days * 24L * 60L * 60L * 1000L):null;
                Date now = new Date();
                if(generationTime > now.getTime()) {
                    System.clearProperty("ponly.license");
                    throw new IllegalStateException(message);
                } else {
                    if(expirationDate != null) {
                        if(expirationDate.getTime() < now.getTime()) {
                            System.clearProperty("ponly.license");
                            throw new IllegalStateException(message);
                        }

                        if(now.getTime() + 1296000000L > expirationDate.getTime()) {
                            String newLicense = __fetchLicense(machineId);
                            if(newLicense != null && !"".equals(newLicense)) {
                                fullLicense = newLicense;
                            }
                        }
                    }

                    System.setProperty("ponly.license", fullLicense);
                }
            } else {
                throw new IllegalStateException(message);
            }
        } catch (Exception var22) {
            var22.printStackTrace();
            throw var22 instanceof IllegalStateException?(IllegalStateException)var22:new IllegalStateException("No invalid license found, please buy license from http://www.ponly.org: " + var22.getMessage());
        }
    }

    private static String __getMachineId() {
        String hostname = null;
        String ethernet = null;
        InetAddress localhost = null;

        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException var11) {
            ;
        }

        try {
            int var8;
            if(localhost == null) {
                hostname = "UNKNOWN";
            } else {
                hostname = localhost.getHostName();
                NetworkInterface ex = NetworkInterface.getByInetAddress(localhost);
                if(!ex.isLoopback() && !ex.isVirtual() && !ex.isPointToPoint() && ex.getHardwareAddress() != null) {
                    byte[] nis = ex.getHardwareAddress();
                    StringBuilder ni = new StringBuilder(nis.length << 1);
                    byte[] var9 = nis;
                    var8 = nis.length;

                    for(int aByte = 0; aByte < var8; ++aByte) {
                        byte bytes = var9[aByte];
                        ni.append(Integer.toHexString(255 & bytes));
                    }

                    ethernet = ni.toString();
                }
            }

            if(ethernet == null) {
                StringBuilder var13 = new StringBuilder();
                Enumeration var14 = NetworkInterface.getNetworkInterfaces();

                label69:
                while(true) {
                    NetworkInterface var15;
                    do {
                        do {
                            do {
                                do {
                                    if(!var14.hasMoreElements()) {
                                        if(1 < var13.length()) {
                                            ethernet = var13.deleteCharAt(var13.length() - 1).toString();
                                        }
                                        break label69;
                                    }

                                    var15 = (NetworkInterface)var14.nextElement();
                                } while(var15.isLoopback());
                            } while(var15.isVirtual());
                        } while(var15.isPointToPoint());
                    } while(var15.getHardwareAddress() == null);

                    byte[] var16 = var15.getHardwareAddress();
                    byte[] var10 = var16;
                    int var18 = var16.length;

                    for(var8 = 0; var8 < var18; ++var8) {
                        byte var17 = var10[var8];
                        var13.append(Integer.toHexString(255 & var17));
                    }

                    var13.append("-");
                }
            }

            if(ethernet == null) {
                throw new IllegalStateException("invalid client");
            } else {
                return Base64.encodeToString(("[P]" + hostname + ":" + ethernet).getBytes(Charset.forName("UTF-8")));
            }
        } catch (SocketException var12) {
            throw new IllegalStateException("");
        }
    }

    private static BigInteger __decodeGroups(String encoded) throws IllegalStateException {
        BigInteger orig = BigInteger.ZERO;
        BigInteger max = BigInteger.valueOf(60466176L);

        int j;
        for(int i = encoded.length(); i >= 0; i = j - 1) {
            j = encoded.lastIndexOf(45, i - 1) + 1;
            String str = encoded.substring(j, i);
            int k = __decodeGroup(str);
            orig = orig.multiply(max);
            orig = orig.add(BigInteger.valueOf((long)k));
        }

        return orig;
    }

    private static int __decodeGroup(String grouped) throws IllegalStateException {
        if(grouped.length() != 5) {
            throw new IllegalStateException();
        } else {
            boolean i = true;
            int j = 0;

            for(int k = grouped.length() - 1; k >= 0; --k) {
                char m = grouped.charAt(k);
                int n;
                if(48 <= m && m <= 57) {
                    n = m - 48;
                } else {
                    if(65 > m || m > 90) {
                        throw new IllegalStateException();
                    }

                    n = m - 65 + 10;
                }

                j *= 36;
                j += n;
            }

            return j;
        }
    }

    private static short __calculateCRC32(String mid, int i, byte[] bytes) {
        CRC32 crc32 = new CRC32();
        int j;
        if(mid != null) {
            for(j = 0; j < mid.length(); ++j) {
                crc32.update(mid.charAt(j));
            }
        }

        crc32.update(i);
        crc32.update(i >> 8);
        crc32.update(i >> 16);
        crc32.update(i >> 24);

        for(j = 0; j < bytes.length - 2; ++j) {
            crc32.update(bytes[j]);
        }

        return (short)((int)crc32.getValue());
    }

    private static String __fetchLicense(String machineId) {
        String licenseUrl = "http://gitool.glanway.com/license";
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String license = null;

        try {
            try {
                conn = (HttpURLConnection)(new URL(licenseUrl)).openConnection();
                conn.setRequestProperty("X-MACHINE", machineId);
                int responseCode = conn.getResponseCode();
                if(200 == responseCode) {
                    reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                    license = reader.readLine();
                }
            } finally {
                if(reader != null) {
                    reader.close();
                }

                if(conn != null) {
                    conn.disconnect();
                }

            }
        } catch (IOException var10) {
            ;
        }

        return license;
    }
}
